/**
 * Created by CAOTE on 11/6/2017.
 */
require('../model/serviceTypeModel.js');
var mongoose = require('mongoose');
var serviceTypeModel = mongoose.model('ServiceType');
var dateUtil = require('../util/dateUtil');
var _ = require('lodash');
exports.updateServiceType = updateServiceType;
exports.getServiceType = getServiceType;
function updateServiceType(data, callback){
    if(_.isEmpty(data)){
        return;
    }
    else if(data.type === 'New'){
        createNewServiceType(data.serviceType,callback);
    }
    else if(data.type === 'Delete'){
        deleteServiceType(data.name,callback)

    }
}
function createNewServiceType(data, callback) {
    data.updateDate = dateUtil.getNowFormatDate();
    serviceTypeModel.find({serviceTypeName: data.serviceTypeName}, function(err, result){
        if(err){
            callback(err, null)
        }else{
            if(result.length>0){
                callback(new Error('Duplicate value'));
            }else{
                serviceTypeModel.create(data, function(err, result){
                    if(err){
                        callback(err, null);
                    }else{
                        callback(null, result)
                    }
                })
            }
        }
    });

}

function getServiceType(callback){
    serviceTypeModel.find({}, function(err, result){
        if(err){
            console.log(err);
        }else{
            callback(err, result);
        }
    })
}



function deleteServiceType(serviceTypeName, callback){
    serviceTypeModel.remove({serviceTypeName: serviceTypeName}, function(err,result){
        if(err){
            callback(err, null);
        }else{
            callback(null, result)
        }
    });
}